package com.school.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.school.entity.HljtClass;
import com.school.entity.HljtDuty;
import com.school.entity.HljtEducation;
import com.school.entity.web.ClassEducation;
import com.school.entity.web.EducationAll;
import com.school.service.impl.HljtClassServiceIpml;
import com.school.service.impl.HljtDutySerivceImpl;
import com.school.service.impl.HljtEducationSerivceImpl;
import com.school.util.Message;
import com.school.util.RepUtil;

@Controller
public class HljtEducationController {

	@Autowired
	private HljtDutySerivceImpl hljtDutySerivceImpl;
	@Autowired
	private HljtEducationSerivceImpl hljtEducationSerivceImpl;
	@Autowired
	private HljtClassServiceIpml hljtClassServiceIpml;
	
	/**
	 * 
	 * @param pageSize
	 * @param pageIndex
	 * @param rep
	 * @return
	 */
	@RequestMapping("/selecteducationall")
	@ResponseBody
	public Message selectEducationall(Integer classid,Integer pageSize,Integer pageIndex,HttpServletResponse rep){
		RepUtil.repPake(rep);
		Message Message=new Message();
		Map<String, Object> data =new HashMap<>();
		if(null !=classid) {
			if(classid>-1) {
				List<HljtClass> HljtClasses=hljtClassServiceIpml.pageFindClassList(classid, null, null,null);
				List<HljtDuty> duty=hljtDutySerivceImpl.findAll();
				List<EducationAll> educationall =hljtEducationSerivceImpl.findEducationAll(classid);
				
				data.put("data", educationall);
				data.put("duty", duty);
				data.put("class", HljtClasses);
				data.put("success", true);
			}else {
				pageIndex=(pageIndex-1)*pageSize;
				List<HljtClass> HljtClasses=hljtClassServiceIpml.pageFindClassList(null, null, null,null);
				List<HljtClass> HljtClassespage=hljtClassServiceIpml.pageFindClassList(null, pageSize, pageIndex,null);
				List<HljtDuty> duty=hljtDutySerivceImpl.findAll();
				List<Object> listall=new ArrayList<>();
				List<EducationAll> all=new ArrayList<>();
				for (int i = 0; i < HljtClassespage.size(); i++) {
					List<EducationAll> educationall =hljtEducationSerivceImpl.findEducationAll(HljtClassespage.get(i).getId());
					if(null != educationall && educationall.size()>0) {
						
						listall.add(educationall);
					}else {
						EducationAll educationAll=new EducationAll();
						educationAll.setClasses(HljtClassespage.get(i).getClasses());
						educationAll.setGrade(HljtClassespage.get(i).getGrade());
						educationAll.setDutyname(null);
						educationAll.setFraction(null);
						educationAll.setNewtime(null);
						educationAll.setRemarks(null);
						
						all.add(educationAll);
						listall.add(all);
					}
				}
				data.put("data", listall);
				data.put("Total", HljtClasses.size());
				data.put("class", HljtClasses);
				data.put("duty", duty);
				data.put("success", true);
			}
		}else {
			data.put("success",  false);
		}
		Message.setData(data);
		return Message;
	}
	
	/**
	 * 德育模式评分分页查找
	 * @param pageSize
	 * @param pageIndex
	 * @param rep
	 * @return
	 */
	@RequestMapping("/selectclasseducation")
	@ResponseBody
	public Message selectClassEducation(Integer pageSize,Integer  pageIndex,HttpServletResponse rep){
		RepUtil.repPake(rep);
		Message Message=new Message();
		Map<String, Object> data =new HashMap<>();
		if(null !=pageSize && null !=pageIndex) {
			List<ClassEducation> ClassEducationList=new ArrayList<>();
			pageIndex=(pageIndex-1)*pageSize;
			List<HljtClass> HljtClasses=hljtClassServiceIpml.pageFindClassList(null, null, null,null);
			List<HljtDuty> HljtDuty=hljtDutySerivceImpl.findAll();
			List<HljtEducation> hljtEducationAll=hljtEducationSerivceImpl.selectHljtEducationAll();
			List<HljtEducation> hljtEducationPage=hljtEducationSerivceImpl.selectHljtEducationPage(pageSize, pageIndex);
			for (int i = 0; i < hljtEducationPage.size(); i++) {
				ClassEducation classEducation=new ClassEducation();
				classEducation.setId(hljtEducationPage.get(i).getId());
				classEducation.setClassid(hljtEducationPage.get(i).getClassid());
				classEducation.setName(hljtEducationPage.get(i).getName());
				classEducation.setRemarks(hljtEducationPage.get(i).getRemarks());
				classEducation.setFraction(hljtEducationPage.get(i).getFraction());
				HljtClass Class=hljtClassServiceIpml.findClass(hljtEducationPage.get(i).getClassid());
				classEducation.setClasses(Class.getGrade());
				classEducation.setLevel(Class.getClasses());
				System.out.println(hljtEducationPage.get(i).getDutyid());
				HljtDuty Duty=hljtDutySerivceImpl.findHljtDuty(hljtEducationPage.get(i).getDutyid());
				classEducation.setDutyname(Duty.getDutyname());
				classEducation.setFullmarks(Duty.getProportion());
				classEducation.setDatetime(hljtEducationPage.get(i).getNewtime());
				ClassEducationList.add(classEducation);
			}
			data.put("HljtEducation", ClassEducationList);
			data.put("data", HljtDuty);
			data.put("class", HljtClasses);
			data.put("Total", hljtEducationAll.size());
			data.put("success", true);
		}else {
			data.put("success",  false);
		}
		Message.setData(data);
		return Message;
	}
	/**
	 * 班级德育模式删除
	 * @param classid
	 * @param rep
	 * @return
	 */
	@RequestMapping("/deleteclasseducation")
	@ResponseBody
	public Message deleteClassEducation(Integer id,HttpServletResponse rep){
		RepUtil.repPake(rep);
		Message Message=new Message();
		Map<String, Object> data =new HashMap<>();
		if(null !=id) {
			hljtEducationSerivceImpl.deleteEducation(id);
			
			
			data.put("success", true);
		}else {
			data.put("success",  false);
		}
		Message.setData(data);
		return Message;
		}
	/**
	 * 班级德育模式添加
	 * @param classid
	 * @param dutyid
	 * @param name
	 * @param fraction
	 * @param remarks
	 * @param rep
	 * @return
	 */
	@RequestMapping("/addclasseducation")
	@ResponseBody
	public Message addClassEducation(Integer classid,Integer dutyid,String name ,Double fraction,String remarks,String newtime,HttpServletResponse rep){
		RepUtil.repPake(rep);
		Message Message=new Message();
		Map<String, Object> data =new HashMap<>();
	
		if(null !=classid && null !=dutyid && null !=name && null!=remarks && null !=fraction) {
			
			HljtEducation hljtEducation=new HljtEducation();
			hljtEducation.setClassid(classid);
			hljtEducation.setDutyid(dutyid);
			hljtEducation.setName(name);
			hljtEducation.setFraction(fraction);
			hljtEducation.setRemarks(remarks);
			hljtEducation.setNewtime(newtime);
			hljtEducationSerivceImpl.addEducation(hljtEducation);
			data.put("success", true);
		}else {
			data.put("success",  false);
		}
		Message.setData(data);
		return Message;
	}
	/**
	 * 班级德育模式修改
	 * @param id
	 * @param dutyid
	 * @param name
	 * @param fraction
	 * @param remarks
	 * @param rep
	 * @return
	 */
	@RequestMapping("/updateclasseducation")
	@ResponseBody
	public Message updateClassEducation(Integer id,Integer classid,String name ,Integer fraction,String remarks,String newtime,HttpServletResponse rep){
		RepUtil.repPake(rep);
		Message Message=new Message();
		Map<String, Object> data =new HashMap<>();
		
		if(null !=id) {
			HljtEducation hljtEducation=new HljtEducation();
			double d=fraction;
			hljtEducation.setId(id);
			hljtEducation.setClassid(classid);
			hljtEducation.setName(name);
			hljtEducation.setFraction(d);
			hljtEducation.setRemarks(remarks);
			hljtEducation.setNewtime(newtime);
			
			hljtEducationSerivceImpl.updateEducation(hljtEducation);
			data.put("success", true);
		}else {
			data.put("success",  false);
		}
		Message.setData(data);
		return Message;
		}
	public static void main(String[] args) {
		int[] a= {2,6,9,1,8};
		int b=0;
		for (int i = 0; i < 5; i++) {
			b=a[i]+b;
		}
		System.out.println(b);
	}
}
